package com.yance.fim.handler;

import com.yance.fim.core.packets.FimPacket;
import com.yance.fim.core.enums.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tio.core.ChannelContext;

/**
 * 客户端消息处理分发器
 *
 * @author yance
 */
@Service
public class MessageHandlerDispatcher {

    private static Logger log = LoggerFactory.getLogger(MessageHandlerDispatcher.class);

    @Autowired
    private ImMessageHandler imMessageHandler;

    @Autowired
    private ConnectorHandler connectorHandler;


    public void dispatchers(FimPacket packet, ChannelContext channelContext) {
        Command command = packet.getHeader().getCommand();
        log.info("start dispatchers " + command.name());
        if (command == Command.SEND) {
            //聊天处理
            imMessageHandler.handler(packet, channelContext);
        } else if (command.ordinal() < Command.SEND.ordinal()) {
            //Websocket连接处理或者Websocket断开连接处理
            connectorHandler.handler(packet, channelContext);
        }
    }
}
